$NG.AllReady(function (
    page,
    {
        useDataIndexChange,
        useValuesChange,
        useUpdateRows,
        useBeforeClick,
        useUpdateRow,
        useClick,
    }
) {
    // 编辑页面逻辑
    const mstform = $NG.getCmpApi("p_form_income_inspection_m");
    const grid = $NG.getCmpApi("p_form_income_inspection_d");
    console.log(mstform, "mstform");
    {//初始化逻辑BEGIN
        if (mstform) {
            console.log(mstform.name + ' is ready');
            PCFilter()
            cntMFilter()
        } else {
            console.error('mstform is not available');
        };
    }//初始化逻辑END


    if (page.oType == "add") {
        var pc = mstform.getItem("phid_pc");
        console.log(pc.getValue());
        console.log("123123");
        if (pc.getValue() > "0") {
            PcZt()
            PCFilter()
            cntMFilter()
            GetPCInfo()
            $NG.execServer("GetCzMInfo", { pc: pc.getValue() }, function (res) {
                const data = JSON.parse(res.data);
                console.log(data);
                const cntid = data[0].extendObjects.cnt;
                const bill_name = data[0].extendObjects.bill_name;

                $NG.updateState((updater) => {
                    updater.data.p_form_income_inspection_m.setProps({
                        u_cnt_name: {
                            value: cntid,
                            label: bill_name
                        },

                    });
                });

                setTimeout(function () {
                    Bycount()
                    var cnt = mstform.getItem("u_cnt_name").getValue();
                    getCzInfo(cnt)
                    var amt = mstform.getItem("amt").getValue();
                    var u_vat_amt_total = mstform.getItem("u_vat_amt_total").getValue();
                    mstform.getItem("u_cnt_prop").setValue(u_vat_amt_total / amt)

                }, 1000);

            });


        }
    }
    if (page.oType == "add" || page.oType == "edit") {
        { 
            // $NG.getCmpApi('toolbar_p_form_income_inspection_d').insert({
            //     id: "czsbqd",
            //     text: "引用设备清单内容",
            //     iconCls: "icon-New"
            // });
        }
    }
    page.oType == 'add' && getPtInfoByPhid()
    function getPtInfoByPhid() {
		if (mstform.getItem("phid_pc").getValue()) {
			$NG.execServer(
				"getPtInfoByPhid",
				{ phid: mstform.getItem("phid_pc").getValue() },
				function (res) {
					const datas = JSON.parse(res.data)[0].extendObjects;
					console.log(`获取项目信息`,datas);
					//帮助字段赋值
					$NG.updateState((updater) => {
						updater.data.p_form_income_inspection_m.setProps({
							u_pc_ywlx:{
                                label:datas.ywlx_name,
                                value:datas.user_ywlx
                            }
						});
                        setTimeout(()=>{
                            hideDkey()
                        },300)
					});
				}
			);
		}
	}
    function hideDkey(){
        const type = mstform.getItem("u_pc_ywlx").getValue()
        const tool = $NG.getCmpApi("toolbar_p_form_income_inspection_d");
        if(type == '8570000000000357'){
            $NG.getCmpApi('toolbar_p_form_income_inspection_d').insert({
                id: "czsbqd",
                text: "引用设备清单内容",
                iconCls: "icon-New"
            });
        } else {
            tool.hideButton("czsbqd")

        }
    }
    // 项目名称变化时自动带入产品线、事业部
    useValuesChange(({ args }) => {
        mstform.getItem("u_cnt_name").setValue("");
        const phid_pc = args[0].phid_pc.value;
        if (phid_pc) {
            console.log(phid_pc);

            Pcstat()
            GetPCInfo(phid_pc)
            PCFilter()
            cntMFilter()
            $NG.execServer("GetCzMInfo", { pc: phid_pc }, function (res) {
                const data = JSON.parse(res.data);
                console.log(data);
                const cntid = data[0].extendObjects.cnt;
                const bill_name = data[0].extendObjects.bill_name;

                $NG.updateState((updater) => {
                    updater.data.p_form_income_inspection_m.setProps({
                        u_cnt_name: {
                            value: cntid,
                            label: bill_name
                        },

                    });
                });

                setTimeout(function () {
                    Bycount()
                    var cnt = mstform.getItem("u_cnt_name").getValue();
                    getCzInfo(cnt)
                    var amt = mstform.getItem("amt").getValue();
                    var u_vat_amt_total = mstform.getItem("u_vat_amt_total").getValue();
                    mstform.getItem("u_cnt_prop").setValue(u_vat_amt_total / amt)

                }, 1000);

            });


        }
        getPtInfoByPhid()
    }, "p_form_income_inspection_m.phid_pc");


    useValuesChange(({ args }) => {
        hideDkey()

    }, "p_form_income_inspection_m.u_pc_ywlx");

    useValuesChange(({ args }) => {
        //    const phid_pc = args[0].phid_pc.value;
        //const cnt = args[0].u_cnt_name.value;
        var cnt = mstform.getItem("u_cnt_name").getValue();
        Bycount()
        getCzInfo(cnt)

    }, "p_form_income_inspection_m.u_cnt_name");
    // 报验支撑名称变化时自动带入产品线、事业部
    useValuesChange(({ args }) => {
        const u_byzckx = args[0].u_byzckx.value;
        if (u_byzckx) {
            console.log(u_byzckx);
            $NG.execServer("getHtTk", { phid: u_byzckx }, function (res) {
                if (res.count == 0) return;
                const data = JSON.parse(res.data);
                console.log(data);
                const u_hkbl = data[0].extendObjects.u_hkbl;

                $NG.updateState((updater) => {
                    updater.data.p_form_income_inspection_m.setProps({
                        u_zckx_prop: u_hkbl,

                    });
                });
            });

        }
    }, "p_form_income_inspection_m.u_byzckx");



    useUpdateRows(({ args, table }) => {
        const mstform = $NG.getCmpApi("p_form_income_inspection_m");
        var gridTab = $NG.getCmpApi('p_form_income_inspection_d'); //获取tab页数据
        const u_jzbcby_amt = mstform.getItem("u_jzbcby_amt").getValue(); //获取值
        const u_jzbcby_amt_vat = mstform.getItem("u_jzbcby_amt_vat").getValue(); //获取值
        const amt = mstform.getItem("amt").getValue(); //获取值

        var sumAmt = 0;
        var sumAmt1 = 0;
        const rows = args[0];
        rows.forEach((items) => {
            if (items.u_bqsp_amt) {
                sumAmt += parseFloat(items.u_bqsp_amt);  //明细
            }
            if (items.u_bpsp_vat_amt) {
                sumAmt1 += parseFloat(items.u_bpsp_vat_amt);  //明细
            }
            //汇总赋值到表头字段
            $NG.updateState((updater) => {
                updater.data.p_form_income_inspection_m.setProps({
                    u_by_amt: sumAmt,  //表头：
                    u_by_vat_amt: sumAmt1,  //表头：
                    u_amt_total: sumAmt + u_jzbcby_amt,  //表头：
                    u_vat_amt_total: sumAmt1 + u_jzbcby_amt_vat,  //表头：
                    u_cnt_prop: (sumAmt1 + u_jzbcby_amt_vat) / amt,  //表头：

                });
            });

        });
        gridTab.updateRow(rows);
    }, 'p_form_income_inspection_d');



    useDataIndexChange(({ args }) => {
        const record = args[0];
        console.log(record)

        record.u_spby = (
            parseFloat(record.u_bqby));

        record.u_nlqty_hbc = (
            parseFloat(record.u_bqby) + parseFloat(record.u_gcl_total));
        record.u_klqty_hbc = (
            parseFloat(record.u_bqby) + parseFloat(record.u_bygcl_total));

        record.u_bqsp_amt = (
            parseFloat(record.u_bqby) * parseFloat(record.u_cnt_prc) / parseFloat(record.u_qdsl) * parseFloat(record.u_cnt_qty));
        record.u_bpsp_vat_amt = (
            parseFloat(record.u_bqby) * parseFloat(record.u_cnt_prc) / parseFloat(record.u_qdsl) * parseFloat(record.u_cnt_qty) * (1 + (parseFloat(record.taxrate))));
        record.u_gcl_prop = (
            parseFloat(record.u_bqby) / parseFloat(record.u_cnt_qty));


        record.u_nl_amt_hbc = (
            parseFloat(record.u_by_amt) + parseFloat(record.u_bqby) * parseFloat(record.u_cnt_prc) / parseFloat(record.u_qdsl) * parseFloat(record.u_cnt_qty) * (1 + (parseFloat(record.taxrate))));
        record.u_kl_amt_hbc = (
            parseFloat(record.u_ljby_amt) + parseFloat(record.u_bqby) * parseFloat(record.u_cnt_prc) / parseFloat(record.u_qdsl) * parseFloat(record.u_cnt_qty) * (1 + (parseFloat(record.taxrate))));

        grid.updateRow(args[0]);

    }, "u_bqby");

    useDataIndexChange(({ args }) => {
        const record = args[0];
        console.log(record)
        if (record.u_spby > record.u_bqby) {
            $NG.alert('审批数量不能大于完成数量');
            record.u_spby = (parseFloat(record.u_bqby));
        }
        else {

            record.u_nlqty_hbc = (
                parseFloat(record.u_spby) + parseFloat(record.u_gcl_total));
            record.u_klqty_hbc = (
                parseFloat(record.u_spby) + parseFloat(record.u_bygcl_total));

            record.u_bqsp_amt = (
                parseFloat(record.u_spby) * parseFloat(record.u_cnt_prc) / parseFloat(record.u_qdsl) * parseFloat(record.u_cnt_qty));

            record.u_bpsp_vat_amt = (
                parseFloat(record.u_spby) * parseFloat(record.u_cnt_prc) / parseFloat(record.u_qdsl) * parseFloat(record.u_cnt_qty) * (1 + (parseFloat(record.taxrate))));

            record.u_nl_amt_hbc = (
                parseFloat(record.u_by_amt) + parseFloat(record.u_spby) * parseFloat(record.u_cnt_prc) / parseFloat(record.u_qdsl) * parseFloat(record.u_cnt_qty) * (1 + (parseFloat(record.taxrate))));
            record.u_kl_amt_hbc = (
                parseFloat(record.u_ljby_amt) + parseFloat(record.u_spby) * parseFloat(record.u_cnt_prc) / parseFloat(record.u_qdsl) * parseFloat(record.u_cnt_qty) * (1 + (parseFloat(record.taxrate))));

            record.u_gcl_prop = (
                parseFloat(record.u_spby) / parseFloat(record.u_cnt_qty));
        }
        grid.updateRow(args[0]);

    }, "u_spby");


    useClick(async function ({ args }) {
        // 获取表单中 phid_pc 的值
        const phidPcValue = mstform.getItem("phid_pc").getValue();

        // 假设 grid 是你的表格组件，并且已经加载了数据
        let gridData = grid.getRows(); // 获取 grid 中的所有行数据

        // 从 grid 数据中提取 u_shsj_name 字段的值
        let existingItems = gridData.map(row => row.u_shsj_name); // 提取所有已有的 u_shsj_name

        // 构建过滤条件字符串，假设使用 SQL IN 操作符进行过滤
        let filterCondition = existingItems.length > 0
            ? `m.phid_pc = '${phidPcValue}' AND d.phid NOT IN ('${existingItems.join("','")}')`
            : `m.phid_pc = '${phidPcValue}'`;

        // 打开帮助选择器并应用过滤条件
        $NG.external.openNGHelp({
            type: "NGMultipleHelp",
            helpId: "cz_sbqd",
            valueField: "phid",
            labelField: "u_equipment_list_name",
            clientSqlFilter: filterCondition // 应用过滤条件
        }).then((data) => {
            console.log(data);

            var gridArr = []; // 初始化 gridArr 数组
            for (var i = 0; i < data.length; i++) {
                // 列出需要赋值的表体字段
                var gridObj = {
                    u_shsj_no: '',
                    u_shsj_name: '',
                    u_detailed_qty: '',
                    u_model: '',
                    u_tech_params: '',
                    u_cnt_qty: '',
                    u_unit: '',
                    prc: '',
                    taxrate: '',
                    u_ly: '',
                    u_srlx: '',
                    u_nfbsl: '',
              
                };
                // 对表体字段进行赋值, 可以取数据库中的值进行赋值
                gridObj.u_srlx = data[i].origin.u_revenue_type;
                gridObj.u_srlx_EXName = data[i].origin.c_name;
                gridObj.u_shsj_no = data[i].origin.u_equipment_list_no;
                gridObj.u_shsj_name = data[i].origin.u_equipment_item;
                gridObj.u_shsj_name_EXName = data[i].origin.u_equipment_list_name;
                gridObj.u_detailed_qty = data[i].origin.u_qty;
                gridObj.u_model = data[i].origin.u_cnt_d_spec;
                gridObj.u_tech_params = data[i].origin.u_desc;
                gridObj.u_cnt_qty = data[i].origin.u_qty;
                gridObj.u_unit = data[i].origin.u_unit;
                gridObj.u_unit_EXName = data[i].origin.msname;
                gridObj.prc = data[i].origin.u_cnt_prc;
                gridObj.taxrate = data[i].origin.u_cnt_d_taxrate;
                gridObj.u_nfbsl = data[i].origin.qdsl;
                gridObj.u_kg_qty = data[i].origin.kglj_qty;
                gridObj.u_kg_amt = data[i].origin.kgljje;
                gridObj.u_year_amt = data[i].origin.nlje;
                gridObj.u_year_qty = data[i].origin.nl_qty;

                gridObj.u_ly = '设备清单手工选择';

                // 关键步骤：将 gridObj 添加到 gridArr 数组中
                gridArr.push(gridObj);
            }
            console.log(gridArr); // 确保这里打印出来的 gridArr 不是空的
            grid.addRows(gridArr); // 将数据添加到表格组件中
        });
    }, "czsbqd");

    useBeforeClick(async function ({ args }) {
        // 按钮点击前事件
        $NG.execServer("GetPcStat", { pc: $NG.getCmpApi('p_form_income_inspection_m').getItem('phid_pc').getValue() }, function (res) {
            if (res.count == 0) return
            const data = JSON.parse(res.data);
            console.log(data);
            const zt = data[0].extendObjects.zt;
            if (zt == 'pau') {
                $NG.alert("此项目为暂停项目，不允许发生此业务！")
                return false
            }
            else if (zt == 'pcl') {
                $NG.alert("此项目为关闭项目，不允许发生此业务！")
                return false
            }
            else {
                return true
            }
        });

    }, "save");

});

function GetPCInfo(pcvalue) {

    $NG.execServer("GetPcCntInfo", { phid_pc: pcvalue }, function (res) {
        if (res.count == 0) return;
        const data = JSON.parse(res.data);
        console.log(data);
        const zz = data[0].extendObjects.zz;
        const zz_oname = data[0].extendObjects.oname;
        const pc_no = data[0].extendObjects.pc_no;
        const cpx = data[0].extendObjects.cpx;
        const cpx_name = data[0].extendObjects.cpx_name;
        const qyzx = data[0].extendObjects.qyzx;
        const qyzx_name = data[0].extendObjects.qyzx_name;
        const htxe = data[0].extendObjects.htxe;
        const user_sf_qf = data[0].extendObjects.user_sf_qf;
        const cwzz = data[0].extendObjects.cwzz;
        const cwzz_name = data[0].extendObjects.cwzz_name;
        console.log(user_sf_qf)
        // $NG.updateState((updater) => {
        //     updater.data.p_form_income_inspection_m.setProps({
        //         phid_org: { value: zz, label: zz_oname },
        //         u_product_line: { value: cpx, label: cpx_name },
        //         u_regional_center: { value: qyzx, label: qyzx_name },
        //         u_pc_no: pc_no,
        //         amt: htxe,
        //         u_sfqf: user_sf_qf
        //     });
        // });
        const isYesNoText = (value) => value === '1' ? '是' : '否';
        $NG.updateState((updater) => {
            updater.data.p_form_income_inspection_m.setProps({
                phid_org: { value: zz, label: zz_oname },
                u_product_line: { value: cpx, label: cpx_name },
                u_regional_center: { value: qyzx, label: qyzx_name },
                u_yszz: { value: cwzz, label: cwzz_name },
                u_pc_no: pc_no,
                amt: htxe,
                u_sfqf: { value: user_sf_qf, label: isYesNoText(user_sf_qf) } // 这里将数值转换为文本描述
            });
        });
    });

}

function getCzInfo(htvalue) {
    const mstform = $NG.getCmpApi("p_form_income_inspection_m");
    const grid = $NG.getCmpApi("p_form_income_inspection_d");
    grid.clearRows();
    console.log('aaaaaaaaaaaaaaaaaaaaaaaaaaaa', mstform.getItem("u_cnt_name").getValue());
    $NG.execServer("GetSrbyInfo_total", { cnt:htvalue }, function (res) {
        console.log(res);
        if (res.count == 0) {
            var pcname = mstform.getItem("phid_pc").getRawValue().label
            $NG.updateState((updater) => {
                updater.data.p_form_income_inspection_m.setProps({
                    bill_name: pcname + '第' + '1' + '次' + "收入报验",
                    u_jzbcby_amt: 0,
                    u_jzbcby_amt_vat: 0,

                });
            });

        }
        else {
            const data = JSON.parse(res.data);
            console.log(data);
            const by_times = data[0].extendObjects.by_times;
            const jzbcbyhs = data[0].extendObjects.jzbcbyhs;
            const jzbcbyws = data[0].extendObjects.jzbcbyws;
            //  const ab = data[0].extendObjects.ab;
            var pcname = mstform.getItem("phid_pc").getRawValue().label
            $NG.updateState((updater) => {
                updater.data.p_form_income_inspection_m.setProps({
                    bill_name: '第' + by_times + '次' + "收入报验",
                    u_jzbcby_amt: jzbcbyws,
                    u_jzbcby_amt_vat: jzbcbyhs,

                });
            });


        }
    });

    $NG.execServer("ByGetPcInfo", { cnt: htvalue }, function (res) {
        console.log('qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq',res);
        if (res.count == 0) return
        const data = JSON.parse(res.data);
        console.log(data);
        const ljcz = data[0].extendObjects.ljcz ?? 0;
        const ljhk = data[0].extendObjects.ljhk ?? 0;
        const ljkpje = data[0].extendObjects.ljkpje ?? 0;
        const ljkpjews = data[0].extendObjects.ljkpjews ?? 0;
      
        $NG.updateState((updater) => {
            updater.data.p_form_income_inspection_m.setProps({
                u_cz_total: ljcz,
                u_hk_amt: ljhk,
                u_kp_amt: ljkpje,
                u_ljkpws: ljkpjews,
                u_ljkpse: ljkpje - ljkpjews,
            });
        });
    });



    $NG.execServer('ByGetCzInfo', { ht: htvalue }, function (res) {
        if (res.count == 0) return;
        const data = JSON.parse(res.data);
        var gridArr = []; // 定义表体数组
        var gridCount = res.count; // 增加的行数, 可根据从数据库中取出的数据总数增行

        for (var i = 0; i < gridCount; i++) {
            // 列出需要赋值的表体字段
            var gridObj = {
                u_income_type: '',
                u_zz_product_cla: '',
                u_list_no: '',
                u_list_name: '',
                u_spec: '',
                u_jscs: '',
                u_sbqd_qty: '',
                u_cnt_qty: '',
                u_cnt_prc: '',
                u_unit: '',
                taxrate: '',
                u_gcl_total: '',
                u_bygcl_total: '',
                u_ljby_amt: '',
                u_bqby: '',
                u_spby: '',
                u_bqsp_amt: '',
                u_bpsp_vat_amt: '',
                u_by_amt: '',
                u_gcl_prop: '',
                u_qdsl: '',
                u_nlqty_hbc: '',
                u_nl_amt_hbc: '',
                u_klqty_hbc: '',
                u_kl_amt_hbc: '',
            };
            gridObj.u_income_type = data[i].extendObjects.u_srlx;
            gridObj.u_income_type_EXName = data[i].extendObjects.srlx_name;
            gridObj.u_list_no = data[i].extendObjects.u_equipment_list_no;
            gridObj.u_list_name = data[i].extendObjects.u_shsj_name;
            gridObj.u_list_name_EXName = data[i].extendObjects.u_equipment_list_name
            gridObj.u_zz_product_cla = data[i].extendObjects.u_self_item_category;
            gridObj.u_zz_product_cla_EXName = data[i].extendObjects.res_bs_codename;
            gridObj.u_jscs = data[i].extendObjects.u_tech_params;
            gridObj.u_unit = data[i].extendObjects.u_msunit;
            gridObj.u_unit_EXName = data[i].extendObjects.msname;
            gridObj.u_spec = data[i].extendObjects.u_model;
            gridObj.u_sbqd_qty = data[i].extendObjects.u_cnt_qty;
            gridObj.u_cnt_qty = data[i].extendObjects.u_cnt_qty;
            gridObj.u_cnt_prc = data[i].extendObjects.prc;
            gridObj.taxrate = data[i].extendObjects.taxrate;
            gridObj.u_bqby = data[i].extendObjects.rest_qty;
            gridObj.u_spby = data[i].extendObjects.rest_qty;
            gridObj.u_bqsp_amt = data[i].extendObjects.prc * data[i].extendObjects.rest_qty / data[i].extendObjects.qdsl * data[i].extendObjects.u_cnt_qty;
            gridObj.u_bpsp_vat_amt = data[i].extendObjects.prc * data[i].extendObjects.rest_qty * (1 + data[i].extendObjects.taxrate) / data[i].extendObjects.qdsl * data[i].extendObjects.u_cnt_qty;
            gridObj.u_gcl_prop = data[i].extendObjects.rest_qty / data[i].extendObjects.ljczsl;
            gridObj.u_by_amt = data[i].extendObjects.current_year_ljbyje;
            gridObj.u_gcl_total = data[i].extendObjects.current_year_ljbygcl;
            gridObj.u_bygcl_total = data[i].extendObjects.ljbygcl;
            gridObj.u_ljby_amt = data[i].extendObjects.ljbyje;
            gridObj.u_qdsl = data[i].extendObjects.qdsl;

            gridObj.u_nl_amt_hbc = data[i].extendObjects.current_year_ljbyje + data[i].extendObjects.prc * data[i].extendObjects.rest_qty * (1 + data[i].extendObjects.taxrate) / data[i].extendObjects.qdsl * data[i].extendObjects.u_cnt_qty;
            gridObj.u_nlqty_hbc = data[i].extendObjects.current_year_ljbygcl + data[i].extendObjects.rest_qty;
            gridObj.u_klqty_hbc = data[i].extendObjects.ljbygcl + data[i].extendObjects.rest_qty;
            gridObj.u_kl_amt_hbc = data[i].extendObjects.ljbyje + data[i].extendObjects.prc * data[i].extendObjects.rest_qty * (1 + data[i].extendObjects.taxrate) / data[i].extendObjects.qdsl * data[i].extendObjects.u_cnt_qty;

            gridArr.push(gridObj);
        }

        // 清空表格内容（放在循环外部）
        grid.clearRows();

        // 调用新增表体行数据的函数
        grid.addRows(gridArr);

        // 更新RichHelp字段（如果需要）
        // 这里假设有一个方法可以刷新或重新渲染表格，以确保RichHelp字段正确显示
        grid.refreshView(); // 或者使用grid.refreshData();
    });



}


function PCFilter() {
    $NG.updateUI((updater, state) => {
        console.log('过滤参数检查', updater);
        updater.form.p_form_income_inspection_m.u_byzckx.setProps({
            onBeforeOpen: async () => {
                var phid_pc = $NG.getCmpApi('p_form_income_inspection_m').getItem('phid_pc').getValue();
                console.log(phid_pc);
                if (!phid_pc) {
                    $NG.alert("请先选择项目");
                    return false;
                } else {
                    return true;
                }
            },
            ...(!!$NG.getCmpApi('p_form_income_inspection_m').getItem('phid_pc').getValue() && {
                clientSqlFilter: {
                    'm.phid_pc': $NG.getCmpApi('p_form_income_inspection_m').getItem('phid_pc').getValue()

                }

            }),
        });

        // console.log(phid_pc);
        console.log($NG.getCmpApi('p_form_income_inspection_m').getItem('phid_pc').getValue());

    });
}


//合同根据项目ID过滤
function cntMFilter() {
    $NG.updateUI((updater, state) => {
        console.log('过滤参数检查', updater);
        updater.form.p_form_income_inspection_m.u_cnt_name.setProps({
            onBeforeOpen: async () => {
                var phid_pc = $NG.getCmpApi('p_form_income_inspection_m').getItem('phid_pc').getValue();
                if (!phid_pc) {
                    $NG.alert("请先选择项目");
                    return false;
                } else {
                    return true;
                }
            },
            ...(!!$NG.getCmpApi('p_form_income_inspection_m').getItem('phid_pc').getValue() && {
                clientSqlFilter: {
                    phid_pc: $NG.getCmpApi('p_form_income_inspection_m').getItem('phid_pc').getValue()
                }
            }),
        });
    });
}


function PcZt() {
    $NG.execServer(
        "GetPcStat",
        { pc: $NG.getCmpApi('p_form_income_inspection_m').getItem('phid_pc').getValue() },
        function (res) {
            if (res.count == 0) return;
            const data = JSON.parse(res.data);
            if (!Array.isArray(data) || data.length === 0 || !data[0].extendObjects) {
                console.error("返回数据格式不正确:", res);
                return;
            }
            const zt = data[0].extendObjects.zt;
            if (zt === 'pau') {
                // $NG.alert(
                //     "此项目为暂停项目，不允许发生此业务！", 
                //     function () { 
                //         $NG.close(); 
                //     }
                // );
                if (confirm("此项目为暂停项目，不允许发生此业务！")) {
                    $NG.close();
                }
            } else if (zt === 'pcl') {
                // $NG.alert(
                //     "此项目为关闭项目，不允许发生此业务！", 
                //     function () {  
                //         $NG.$NG.closeTab()(); 
                //     }
                // );
                if (confirm("此项目为关闭项目，不允许发生此业务！")) {
                    $NG.close();
                }
            }


        }
    );
}

function Pcstat() {
    $NG.execServer("GetPcStat", { pc: $NG.getCmpApi('p_form_income_inspection_m').getItem('phid_pc').getValue() }, function (res) {
        if (res.count == 0) return
        const data = JSON.parse(res.data);
        console.log(data);
        const zt = data[0].extendObjects.zt;
        if (zt == 'pau') {
            $NG.alert("此项目为暂停项目，不允许发生此业务！")
        }
        else if (zt == 'pcl') {
            $NG.alert("此项目为关闭项目，不允许发生此业务！")
        }
    });
}


function Bycount() {
    $NG.execServer(
        "getByNACount",
        { cnt: $NG.getCmpApi('p_form_income_inspection_m').getItem('u_cnt_name').getValue() }, function (res) {
          
            if (res.count == 0) return;
            const data = JSON.parse(res.data);
            if (!Array.isArray(data) || data.length === 0 || !data[0].extendObjects) {
                console.error("返回数据格式不正确:", res);
                return;
            }
            const sl = data[0].extendObjects.sl;
            if (sl > 0) {
                async function handleConfirm() {
                    const confirmed = await $NG.confirm("此项目存在未审核的收入报验单，不允许新增收入报验单，请先处理历史单据！");
                    if (confirmed) {
                        $NG.close();
                        return;
                    }
                }
                
                handleConfirm();
            }


        }
    );
}






